﻿using Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class CanvasTool {
    [MenuItem("工具/UI画布", priority = 2050)]
    static void BuildUICanvas()
    {
        InitCanvas();
    }
        
    private static void InitCanvas()
    {
        GameObject goGamera = new GameObject("UICamera", typeof(Camera));
        Camera camera = goGamera.GetComponent<Camera>();
        camera.clearFlags = CameraClearFlags.Depth;
        camera.backgroundColor = Color.black;
        camera.cullingMask = Layer.UIMask;
        camera.depth = 0;
        camera.orthographic = true;
            
        GameObject gameObject = new GameObject("Canvas", typeof(RectTransform));
        gameObject.layer = Layer.UI;
        RectTransform transform = gameObject.transform as RectTransform;
        Canvas canvas = gameObject.AddComponent<Canvas>();
        canvas.renderMode = RenderMode.ScreenSpaceCamera;
        canvas.worldCamera = camera;
        CanvasScaler scaler = gameObject.AddComponent<CanvasScaler>();
        scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
        scaler.matchWidthOrHeight = 1f;
        scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
        scaler.referenceResolution = new Vector2(Config.PreferResolutionWidth, Config.PreferResolutionHeight);
        gameObject.AddComponent<GraphicRaycaster>();
            
        int[] allName = UILayerName.AllName;
        foreach (int name in allName)
        {
            UILayerFcatory.CreateUILayer(name, transform);
        }
    }
}
